package com.zhs.gccs.view.taskMap;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.zhs.gccs.R;
import com.zhs.gccs.mapbox.MapLegend;
import com.zhs.gccs.view.BaseView;

public class LegendLayerView extends BaseView {
    private View ll_item;
    private ImageView img;
    private TextView txt;
    private int position;
    public LegendLayerView(@NonNull Context context) {
        super(context);
    }

    public LegendLayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LegendLayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_view_legend_layer,
                this, true);
        ll_item = view.findViewById(R.id.ll_item);
        img = view.findViewById(R.id.img);
        txt = view.findViewById(R.id.txt);
        ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(operationListener!=null)operationListener.onItemClick();
            }
        });
    }

    public void setData(MapLegend data,int position) {
        this.position = position;
        String count = data.getCount()!=null?"("+data.getCount()+")":"";
        txt.setText(data.getTag()+count);
        if(data.isSelected()){
            Glide.with(context).load(data.getSelectedImg()).into(img);
        }else {
            Glide.with(context).load(data.getImg()).into(img);
        }
    }

    public static interface OperationListener{
        void onItemClick();
    }
    private OperationListener operationListener;

    public OperationListener getOperationListener() {
        return operationListener;
    }

    public void setOperationListener(OperationListener operationListener) {
        this.operationListener = operationListener;
    }
}
